<template>
  <div class="cu-common-layout">
    <div class="cu-common-layout-center">
<!--      <el-row class="cu-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>-->
      <div class="cu-common-layout-main cu-flex-main">
        <div class="cu-common-head">
          <div>
            <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()" v-has="'btn_add'">新增</el-button>
            <el-button type="text" icon="el-icon-download" @click="exportData()" >导出规则</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="handleStatusChange()" v-has="'btn_status'">更改状态</el-button>
          </div>
          <div class="cu-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh cu-common-head-icon" :underline="false" @click="reset()" />
            </el-tooltip>
            <screenfull isContainer />
          </div>
        </div>
        <custom-table v-loading="listLoading" :data="list" @sort-change="sortChange" @selection-change="handleSelectionChange" :default-sort="{ prop: 'sort', order: 'ascending' }">
          <!-- <el-table-column prop="code" label="规则编码" header-align="center" align="center" v-if="custom.hasP('code')" width="80" /> -->
          <el-table-column prop="name" label="规则名称" header-align="center" align="left" v-if="custom.hasP('name')" width="150" />
          <el-table-column prop="type" label="规则类型" align="center" v-if="custom.hasP('type')" width="80">
            <template slot-scope="scope">
              {{ scope.row.type | dynamicText(typeOptions) }}
            </template>
          </el-table-column>
          <el-table-column prop="points" label="分值" sortable="custom" header-align="center" align="right" v-if="custom.hasP('points')" width="80" />
          <el-table-column prop="status" label="启用状态" align="center" v-if="custom.hasP('status')" width="100">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status" key="启用" type="success" effect="dark">
                启用
              </el-tag>
              <el-tag v-else key="关闭" type="danger" effect="dark">
                关闭
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="describe" label="规则描述" header-align="center" align="left" v-if="custom.hasP('describe')" />
          <el-table-column prop="sort" label="排序" sortable="custom" align="center" v-if="custom.hasP('sort')" width="80" />
          <!-- <el-table-column prop="createTime" label="创建时间" align="center" v-if="custom.hasP('createTime')"
            :formatter="custom.tableDateFormat" width="100" />
          <el-table-column prop="createUserName" label="创建用户" align="left" v-if="custom.hasP('createUserName')"
            width="100" /> -->
          <el-table-column prop="updateTime" label="修改时间" align="center" v-if="custom.hasP('updateTime')" :formatter="custom.tableDateFormat" width="100" />
          <el-table-column prop="updateUserName" label="修改用户" align="left" v-if="custom.hasP('updateUserName')" width="100" />
          <el-table-column label="操作" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)" v-has="'btn_edit'">编辑</el-button>
              <el-button type="text" @click="handleDel(scope.row.id)" class="Manage-table-delBtn" v-has="'btn_remove'">删除</el-button>
            </template>
          </el-table-column>
        </custom-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
      </div>
    </div>
    <custom-form v-if="formVisible" ref="CustomForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
  </div>
</template>
<script>
import request from '@/utils/request'
import CustomForm from './Form'
import ExportBox from './ExportBox/index.vue'
export default {
  components: { CustomForm, ExportBox },
  data() {
    return {
      query: {},
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'sort'
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'sort', label: '排序' },{ prop: 'name', label: '规则名称' }, { prop: 'type', label: '规则类型' }, { prop: 'points', label: '分值' }, { prop: 'status', label: '启用状态' }, { prop: 'describe', label: '规则描述' } ],
      typeOptions: [{ fullName: '人工操作', id: 'Artificial' }, { fullName: '系统操作', id: 'System' }]
    }
  },
  computed: {},
  created() {
    this.initData()
  },
  methods: {
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query }
      // let query = {}
      request({
        url: `api/campus/integral/actions/rule/export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.open(this.define.comUrl + res.data.url)
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/integral/rule/list`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = this.reduceSame(val)
    },
    // 数组对象去重
    reduceSame(arr) {
      let obj = {}
      return arr.reduce(function(prev, item) {
        obj[item.id] ? '' : (obj[item.id] = 1 && prev.push(item))
        return prev
      }, [])
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/campus/integral/rule/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    },
    handleStatusChange() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('确认要更改规则的状态吗?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            this.listLoading = true
            var ids = this.multipleSelection
              .map(m => {
                return m.id
              })
              .join(',')
            console.log(ids)
            this.$nextTick(() => {
              request({
                url: `/api/campus/integral/rule/status/${ids}`,
                method: 'PUT'
              }).then(res => {
                this.$message({
                  type: 'success',
                  message: res.msg,
                  onClose: () => {
                    this.listLoading = false
                    this.search()
                  }
                })
              })
            })
          })
          .catch(() => {
            //取消
            row.status = row.status ? 0 : 1
            this.listLoading = false
          })
      } else {
        this.$message({
          type: 'warning',
          message: '请选择需要操作的数据',
          onClose: () => {
            this.initData()
          }
        })
      }
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'sort'
      }
      this.initData()
    },

    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
      this.listQuery.sidx = !order ? '' : prop
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'sort'
      }
      this.initData()
    }
  }
}
</script>
